package com.tianhui.consignortwofei.mvp.ui.activity.display;

import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.itemView.DateItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.mvp.model.StatisticsModel;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/6/15
 * @description
 **/
public abstract class BaseDisplayDataActivity extends BaseActivity {
    @BindView(R.id.activity_display_data_startDateItemView)
    DateItemView startDateItemView;
    @BindView(R.id.activity_display_data_endDateItemView)
    DateItemView endDateItemView;
    protected StatisticsModel statisticsModel;
    private String startDate = "";
    private String endDate = "";
    private int year;
    private int month;
    private int currentMonth;

    @Override
    public void init() {
        setTitle("数据展示");
        statisticsModel = new StatisticsModel();

        initDate();

        initListener();
        initData();

        searchData(startDate, endDate);
    }

    abstract void initData();
    abstract void searchData(String startDate, String endDate);

    private void initListener() {
        startDateItemView.setOnSelectDateListener(new DateItemView.OnSelectDateListener() {
            @Override
            public void onSelectDate(String date) {
                startDate = date;
                searchData(startDate, endDate);
            }
        });
        endDateItemView.setOnSelectDateListener(new DateItemView.OnSelectDateListener() {
            @Override
            public void onSelectDate(String date) {
                endDate = date;
                searchData(startDate, endDate);
            }
        });

    }

    private void initDate() {
        startDate = DateUtil.getFirstDayOfCurrentMonth();
        endDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        currentMonth = month;

        startDateItemView.setContent(startDate);
        endDateItemView.setContent(endDate);
    }

    @OnClick(R.id.activity_display_data_lastMonthTextView)
    public void lastMoth() {

        month--;

        startDate = DateUtil.getFirstDayOfMonth(year, month);
        endDate = DateUtil.getLastDayOfMonth(year, month);

        searchData(startDate, endDate);

        startDateItemView.setContent(startDate);
        endDateItemView.setContent(endDate);

    }

    @OnClick(R.id.activity_display_data_nextMonthTextView)
    public void nextMoth() {
        if (month == currentMonth) {
            showToast("已经是最新一个月了");
        } else {

            month++;

            if (month == currentMonth) {
                startDate = DateUtil.getFirstDayOfCurrentMonth();
                endDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");
            } else {
                startDate = DateUtil.getFirstDayOfMonth(year, month);
                endDate = DateUtil.getLastDayOfMonth(year, month);
            }
            searchData(startDate, endDate);
        }
        startDateItemView.setContent(startDate);
        endDateItemView.setContent(endDate);
    }
}
